//
//  UIView+ZSMasonry.m
//  Epayment
//
//  Created by safiri on 2018/6/13.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "UIView+ZSMasonry.h"
#import <Masonry/Masonry.h>

@implementation UIView (ZSMasonry)

- (void)distributeSpacingHorizontally:(NSArray<UIView *>*)views withSpace:(CGFloat)space andEdge:(UIEdgeInsets)edgeInsets{
    
    NSInteger count = views.count;
    for (NSInteger i=0; i<count; i++) {
        UIView *view = views[i];
        [self addSubview:view];
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.equalTo(self.mas_left).offset(edgeInsets.left);
            }else {
                make.left.equalTo(views[i-1].mas_right).offset(space);
            }
            if (i == count-1) {
                make.right.equalTo(self.mas_right).offset(-edgeInsets.right);
            }
            make.top.equalTo(self.mas_top).offset(edgeInsets.top);
            make.bottom.equalTo(self.mas_bottom).offset(-edgeInsets.bottom);
            if (i-1>=0) {
                make.width.mas_equalTo(views[i-1]);
            }
        }];
    }
}

- (void)distributeSpacingHorizontally:(NSArray<UIView *>*)views SpaceH:(CGFloat)spaceH SpaceV:(CGFloat)spaceV Columns:(NSInteger)columns Edge:(UIEdgeInsets)edgeInsets  {
    
    NSInteger count = views.count;
    for (NSInteger i=0; i<count; i++) {
        UIView *view = views[i];
        [self addSubview:view];
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (i%columns == 0) {
                make.left.equalTo(self.mas_left).offset(edgeInsets.left);
                if (i == 0) {
                    make.top.equalTo(self.mas_top).offset(edgeInsets.top);
                }else {
                    make.top.equalTo(views[i-1].mas_bottom).offset(spaceV);
                }
                if (i+columns >= count) {
                    make.bottom.equalTo(self.mas_bottom).offset(-edgeInsets.bottom);
                }
            }else {
                make.left.equalTo(views[i-1].mas_right).offset(spaceH);
                make.top.equalTo(views[i-1].mas_top);
            }
            
            if (i == columns*(i/columns)+(columns-1)) {
                make.right.equalTo(self.mas_right).offset(-edgeInsets.right);
            }
            if (i-1>=0) {
                make.width.mas_equalTo(views[i-1]);
                make.height.mas_equalTo(views[i-1]);
            }
        }];
    }
}

- (void)distributeEqualSpacingHorizontallyWith:(NSArray*)views
{
    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
    
    for ( NSInteger i = 0 ; i < views.count+1 ; ++i )
    {
        UIView *v = [UIView new];
        [spaces addObject:v];
        [self addSubview:v];
        
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(v.mas_height);
        }];
    }
    
    UIView *v0 = spaces[0];
    
    [v0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.centerY.equalTo(((UIView*)views[0]).mas_centerY);
    }];
    
    UIView *lastSpace = v0;
    for ( NSInteger i = 0 ; i < views.count; ++i )
    {
        UIView *obj = views[i];
        UIView *space = spaces[i+1];
        
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastSpace.mas_right);
        }];
        
        [space mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(obj.mas_right);
            make.centerY.equalTo(obj.mas_centerY);
            make.width.equalTo(v0);
        }];
        
        lastSpace = space;
    }
    
    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right);
    }];
    
}

- (void)distributeEqualSpacingVerticallyWith:(NSArray*)views
{
    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
    
    for ( NSInteger i = 0 ; i < views.count+1 ; ++i )
    {
        UIView *v = [UIView new];
        [spaces addObject:v];
        [self addSubview:v];
        
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(v.mas_height);
        }];
    }
    
    
    UIView *v0 = spaces[0];
    
    [v0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.centerX.equalTo(((UIView*)views[0]).mas_centerX);
    }];
    
    UIView *lastSpace = v0;
    for ( NSInteger i = 0 ; i < views.count; ++i )
    {
        UIView *obj = views[i];
        UIView *space = spaces[i+1];
        
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastSpace.mas_bottom);
            make.leading.trailing.equalTo(self);
        }];
        
        [space mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(obj.mas_bottom);
            make.centerX.equalTo(obj.mas_centerX);
            make.height.equalTo(v0);
        }];
        
        lastSpace = space;
    }
    
    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
    }];
    
}

@end
